// TOWN SCRIPT
//    Town 1: Ritual Grounds

// This is the special encounter script for this town.
// The states INIT_STATE, EXIT_STATE, and START_STATE have
// meanings that are described in the documenation. States you write
// yourself should be numbered from 10-100.

//FLAGS
//(1,0) = state 0: arrival
//(1,1) = state 0: party > 1
//(1,2) = state 11: can see center
//(1,3) = state 12: spinning pentagram
//(1,5) = demon dead

begintownscript;

variables;
int i,j,k,lvl,choice;

body;

beginstate INIT_STATE;
// This state called whenever this town is entered.
	if (party_size() > 1) {
		try_to_split_party(2,4,0);
		set_flag(1,1,1);
	}
	play_sound(10);

	if (get_flag(1,0) == 0) {
		message_dialog("It is dark here. All you can see is a narrow, spiraling path of runes. It appears all you can do is see where it takes you...","");
		set_flag(1,0,1);
	}

//Names

	set_crime_tolerance(5);

break;

beginstate EXIT_STATE;
// Always called when the town is left.
break;

beginstate START_STATE;
break;

beginstate 10;
	reset_dialog();
	add_dialog_str(0,"Are you ready to return to the Registration Temple?",0);
	add_dialog_choice(0,"Not yet.");
	add_dialog_choice(1,"Yes, I'm ready.");
	choice = run_dialog(0);
	if (choice == 1){
		block_entry(1);
		end();
	}
	if (choice == 2){
		play_sound(10);
		if (get_flag(1,1) > 0) {
			message_dialog("Your party is waiting for you upon your return.","");
			reunite_party();
		}
		move_to_new_town(0,14,7);
	}
break;

beginstate 11;
	if (get_flag(1,2) == 0){
		message_dialog("As you follow the rune spiral path, you can see there is a pentagram at its center. That must be where the demon is.","");
		set_flag(1,2,1);
	}
	if ((get_flag(1,3) == 1) && (get_flag(1,5) == 0)){
		message_dialog("No turning back now, you have to finish the ritual!","");
		block_entry(1);
	}
break;

beginstate 12;
	if (get_flag(1,3) == 0){
		force_view_center(16,16);
		put_boom_on_space(15,15,6,0);
		put_boom_on_space(16,15,6,0);
		put_boom_on_space(15,16,6,0);
		put_boom_on_space(16,16,6,0);
		run_animation_sound(54);
		set_terrain(15,15,427);
		set_terrain(16,15,428);
		set_terrain(15,16,429);
		set_terrain(16,16,430);
		force_instant_terrain_redraw();
		pause(5);
		message_dialog("You are slowly approaching the spiral's center, when suddenly, the pentagram begins to spin. The final stage of the ritual has begun.","");
		force_view_center(17,23);
		set_flag(1,3,1);
	}
break;

beginstate 13;
	if (get_flag(1,4) == 0){
		force_view_center(16,16);
		put_jagged_zap(15,15,16,16,2);
		put_jagged_zap(16,15,16,16,2);
		put_jagged_zap(15,16,16,16,2);
		put_jagged_zap(16,16,16,16,2);
		run_animation_sound(53);
		spawn_creature(6);
		force_instant_terrain_redraw();
		pause(5);
		message_dialog("You have almost reached the center when, in a final flash of lightning, the demon appears within the pentagram. It grins at you, daring you to move closer.","_Approach, mortal. You haven't come all this way just to be a coward. It is time to face your destiny. We'll see if you're worthy to be a real adventurer!_");
		force_view_center(11,15);
		set_flag(1,4,1);
	}
break;